package com.core.util;

import jxl.write.biff.*;
import jxl.write.*;
import jxl.*;

import java.util.*;
import java.io.*;
import com.core.configuration.*;

/**
 * Excel操作服务类
 * 
 * 
 */
public class ExcelUtil {

	/**
	 * 生成Excel文件
	 * 
	 * @param content
	 * @param folder
	 * @return
	 */
	public static String generateExcelFile(List<String[]> content, String folder) {
		String ret = "";

		WritableWorkbook wwb = null;
		try {
			String filename = FileUtil.renameFileName("excel.xls");
			String directory = Configuration.UPLOAD_FOLDER
					+ folder
					+ FileUtil
							.createDirectoryAutoManage(Configuration.UPLOAD_FOLDER
									+ folder);

			ret = directory + FileUtil.SEPRATOR + filename;

			wwb = Workbook.createWorkbook(new java.io.File(ret));
			WritableSheet ws = wwb.createSheet("sheet1", 0);

			int len = content.get(0).length;
			for (int i = 0; i < content.size(); i++) {
				String[] c = content.get(i);
				for (int j = 0; j < len; j++) {
					Label labelC = new Label(j, i, c[j]);
					try {
						ws.addCell(labelC);
					} catch (Exception e) {
					}
				}
			}

			try {
				wwb.write();
			} catch (Exception e) {
			}
		} catch (Exception e) {
			e.printStackTrace();
		}

		finally {
			if (wwb != null) {
				try {
					wwb.close();
				} catch (Exception e) {
					wwb = null;
				}
			}
		}
		return ret;
	}
}
